package com.example.phpclient.student;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.phpclient.R;
import com.example.phpclient.Student;

import org.json.JSONObject;

import java.util.HashMap;


public class MainStudent extends AppCompatActivity {
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    RequestQueue queue;
    @Override
    protected void onCreate( Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.stu_main);
        queue = Volley.newRequestQueue(getApplicationContext());
        TextView textView =(TextView)findViewById(R.id.text_stu);
        Button button_qd=(Button)findViewById(R.id.btn_stu_qd);
        Button button_score=(Button)findViewById(R.id.btn_stu_score);
        Button button_area=(Button)findViewById(R.id.btn_stu_area);
        pref = getSharedPreferences("data",MODE_PRIVATE);
        jsonRequest("find_stuo",pref.getString("userNo",""));

        button_qd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainStudent.this, QdStudent_activity.class);
                startActivity(intent);
            }
        });
        button_score.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainStudent.this, ScoreStudent_activity.class);
                startActivity(intent);
            }
        });
    }

    public void jsonRequest(String aim, final String userNo){//通过userNo 查询student信息
        String url= this.getString(R.string.recUrl);

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("forwhat",aim);
        params.put("userNo",userNo);
        JSONObject jsonObject = new JSONObject(params);
        Log.d("Tag",jsonObject.toString());
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Student stuo = JSON.parseObject(jsonObject.toString(),Student.class);//解析json
                        Log.d("TAG1",jsonObject.toString());
                        if (userNo.equals(stuo.getS_no())){//java判断字符串相等不能用“==”
                            //存储到pref
                            SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                            editor.putString("s_no",stuo.getS_no());
                            editor.putString("s_name",stuo.getS_name());
                            editor.putString("s_class",stuo.getS_class());
                            editor.putString("s_sex",stuo.getS_sex());
                            editor.apply();
                            TextView textView =(TextView)findViewById(R.id.text_stu);
                            textView.setText(stuo.getS_name()+" !");//textView在网络请求完毕后，更新到ui
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError");
                    }
                });
        queue.add(jsObjRequest);
    }
}
